package topcoder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class PowerOutage {

	private HashMap<Integer,List<Integer[]>> map=new HashMap<Integer, List<Integer[]>>();
	
	public int estimateTimeOut(int[] from,int[] to,int[] duct)
	{
		for(int i=0;i<from.length;i++)
		{
			if(map.containsKey(from[i]))
			{
				List<Integer[]> list=map.get(from[i]);
				list.add(new Integer[]{to[i],duct[i]});
			}else{
				List<Integer[]> list=new ArrayList<Integer[]>();
				list.add(new Integer[]{to[i],duct[i]});
				map.put(from[i], list);
			}
		}
		int max=findMaxRoute(0);
		
		int result=0;
		
		for(int v:duct)result+=2*v;
				
		
		return result-max;
	}
	
	private int findMaxRoute(int point)
	{
		if(map.containsKey(point))
		{
			int tmp=0;
			for(Integer v[]:map.get(point))
			{
				int r=v[1]+findMaxRoute(v[0]);
				if(r>tmp)tmp=r;
			}
			return tmp;
		}
		return 0;
	}
	
	
/*	
	int[] f;
	int[] t;
	int[] l;
	public int estimateTimeOut(int[] fromJunction, int[] toJunction, int[] ductLength) {
	   f=fromJunction;
	   t=toJunction;
	   l=ductLength;
	   int maxRoute=0;
	   int min=0;
	   for(int i=0;i<t.length;i++){
	    if(isLeaf(t[i])){
	     if(distanceToRoot(t[i])>maxRoute){
	      maxRoute=distanceToRoot(t[i]);
	     }
	    }
	   }
	   for(int j=0;j<l.length;j++){
	    min=min+l[j];
	   }
	   System.out.println(maxRoute);
	   return 2*min-maxRoute;
	}
	private boolean isLeaf(int p){
	   for(int i=0;i<f.length;i++){
	    if(p==f[i])return false;
	   }
	   return true;
	}

	private int distanceToRoot(int p){
	   int distance=0;
	   int n;
	   n=search(p,t);
	   distance=distance+l[n];
	   while(f[n]!=0){
	    n=search(f[n],t);
	    distance=distance+l[n];
	   }
	   return distance;
	  
	}
	private int search(int p,int[] array){
	   for(int i=0;i<array.length;i++){
	    if(p==array[i])return i;
	   }
	   return -1;
	}
*/
	
	
	public static void main(String[] args) {
//		int from[]={0, 0, 0, 1, 1, 0, 3, 5, 1, 5, 7, 5, 9, 8, 11, 10, 5, 8, 15, 1, 0, 9, 20, 0, 4, 4, 9, 22, 27, 5, 29, 19, 25, 18, 3, 21, 35, 22, 37, 13, 14, 17, 4, 2, 16, 19};
//		int to[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46};
//		int duct[]={379499, 166690, 1343134, 1641741, 746252, 422653, 505011, 196589, 967672, 1306817, 848735, 1475454, 230577, 524420, 337691, 1771612, 692558, 1358483, 580331, 1212494, 200537, 1064259, 184, 1105877, 246146, 1860087, 1404521, 1412698, 1663214, 6438, 1113183, 488144, 418060, 1386673, 1911684, 917363, 1520221, 221901, 848402, 782427, 1070163, 1477933, 19582, 1355209, 1669785, 1321542};
		
		int[][] input=new int[][]{
				{5,   0,    0,   0,   11,  4,    1,  1,    1,    1,   0,     0,   6,    0}, 
				{7,   3,   13,  8,   14,  6,   5,   9,   11,   12, 1,     4,   10,  2}, 
				{68, 26, 13, 65,  25, 75, 11, 98, 32,   8,   39,   71,  70,  37}};


		
		int from[]={0,0,0,1,1,2,5,5};
		int to[]={1,2,3,4,7,5,6,8};
		int duct[]={1,5,100,1,1,2,4,1};
		System.out.println(new PowerOutage().estimateTimeOut(input[0],input[1],input[2]));
		
		
	}
}
